package com.durian.controller;

import com.durian.domain.DTO.ObsAddressAddDTO;
import com.durian.domain.DTO.ObsAddressUpdateDTO;
import com.durian.domain.DTO.ObsDefaultListDTO;
import com.durian.service.IObsAddressService;
import com.durian.utils.Result;
import jakarta.validation.constraints.Positive;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户地址管理
 *
 * @author Durian
 * @since 2023-11-23
 */
@Validated
@RestController
@RequestMapping("book/address")
@RequiredArgsConstructor
public class ObsAddressController {

    private final IObsAddressService addressService;

    /**
     * 新增地址
     */
    @PostMapping("add")
    public Result addAddress(
            @Validated
            @RequestBody
            ObsAddressAddDTO addressAddDTO
    ) {
        addressService.addAddress(addressAddDTO);
        return Result.success();
    }

    /**
     * 移除地址
     */
    @PutMapping("remove")
    public Result removeAddress(
            @RequestBody
            List<@Positive(message = "id格式有误") Long> ids
    ) {
        addressService.removeAddress(ids);
        return Result.success();
    }

    /**
     * 更新地址
     */
    @PutMapping("update")
    public Result updateAddress(
            @Validated
            @RequestBody
            ObsAddressUpdateDTO addressUpdateDTO
    ) {
        addressService.updateAddress(addressUpdateDTO);
        return Result.success();
    }

    /**
     * 获取地址列表
     */
    @PostMapping("list")
    public Result listAddress(
            @Validated
            @RequestBody
            ObsDefaultListDTO defaultListDTO
    ) {
        return Result.success(addressService.listAddress(defaultListDTO));
    }

    /**
     * 获取地址列表
     */
    @GetMapping("list/{userId}")
    public Result listAddress(
            @PathVariable
            Long userId
    ) {
        return Result.success(addressService.listAddress(userId));
    }

}
